﻿using Infragistics.Win.UltraWinGrid;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using Titan.FIS.Barcode.TwoBarcode;
using Titan.FIS.Enum;
using Titan.FIS.UI;
using Titan.WinFormSys;

namespace Titan.FIS.SampleRecd
{
    public partial class frmLmsMain : FrmBase
    {
        private const bool _isMH = true;

        private string _strSendUserID = string.Empty;//送样人ID号
        private float _iBottle2;
        private float _iBottle3;
        private float _iBottle6;
        private bool _isCheckWeight = true;

        //是否打印标签数字
        private int _iPrintTestCodeNum = 0;
        //是否自动打印化验编码
        private int _iAutoRecvAndPrintTestCode = 0;
        //打印条码类型
        private int _iPrintBarcodeType = 2;

        private readonly IList<UltraGridRow> _listSelectRow = new List<UltraGridRow>();//选中的设备，用于打印

        /// <summary>
        /// 当前化验室
        /// </summary>
        private LaboratoryType _LabType = LaboratoryType.RL;

        /// <summary>
        /// 构造函数
        /// </summary>
        public frmLmsMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmLMSMain_Load(object sender, EventArgs e)
        {
            dtDate.Value = DateTime.Now;
            //dtDate.Checked = false;

            Common.UserInfo = UserInfo;
            Common.MsgBox = ricMsg;
            this.ActiveControl = txtCode;
            txtCode.Focus();
            string strLmsLabType = System.Configuration.ConfigurationManager.AppSettings["LMS_LAB"];
            //入厂煤和入炉煤完全分开
            if (strLmsLabType == "RC")
            {
                _LabType = LaboratoryType.RC;
            }

            try
            {
                _iPrintTestCodeNum = int.Parse(ConfigurationManager.AppSettings["PrintTestCodeNum"]);
                _iAutoRecvAndPrintTestCode = int.Parse(ConfigurationManager.AppSettings["AutoRecvAndPrintTestCode"]);
                _iPrintBarcodeType = int.Parse(ConfigurationManager.AppSettings["PrintBarcodeType"]);
            }
            catch (Exception ex)
            {
                MessageBox.Show("配置文件中的RFID读卡器端口配置异常，请输入有效整形数据，谢谢！", ex.Message);
                return;
            }
            //DoLoadBottle();
        }

        /// <summary>
        /// 获取样瓶重量信息
        /// </summary>
        private void DoLoadBottle()
        {
            DataTable tabSource = null;
            var errMsg = DataOperate.DataQuery.DoQuerySampleBottleWeight(ref tabSource);
            if (string.IsNullOrEmpty(errMsg))
            {
                if (tabSource != null && tabSource.Rows.Count > 0)
                {
                    foreach (DataRow dr in tabSource.Rows)
                    {
                        switch (dr["PARAMETER_KEY"].ToString())
                        {
                            case "Bottle2":
                                _iBottle2 = int.Parse(dr["PARAMETER_VALUE"].ToString());
                                break;
                            case "Bottle3":
                                _iBottle3 = int.Parse(dr["PARAMETER_VALUE"].ToString());
                                break;
                            case "Bottle6":
                                _iBottle6 = int.Parse(dr["PARAMETER_VALUE"].ToString());
                                break;
                        }
                    }
                }
                else
                {
                    FormHelper.ApplicationShow(ricMsg, DisplayType.Error, "查询样瓶重量信息失败！原因：未找到对应的参数！");
                }
            }
            else
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("查询样瓶重量信息失败！原因：{0}", errMsg));
            }

        }

        /// <summary>
        /// 表格点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GridMain_Click(object sender, EventArgs e)
        {
            if (GridMain.ActiveRow != null)
            {
                try
                {
                    GridMain.ActiveRow.Cells["CHECK"].Value = !(bool)GridMain.ActiveRow.Cells["CHECK"].Value;
                    if ((bool)GridMain.ActiveRow.Cells["CHECK"].Value)
                    {
                        _listSelectRow.Add(GridMain.ActiveRow);
                    }
                    else
                    {
                        _listSelectRow.Remove(GridMain.ActiveRow);
                    }
                }
                catch//默认值不是有效的布尔值，故会异常，也就是第一次选择，即设置为真
                {
                    GridMain.ActiveRow.Cells["CHECK"].Value = true;
                    _listSelectRow.Add(GridMain.ActiveRow);
                }
                GridMain.ActiveRow.Activated = false;
            }
        }

        /// <summary>
        /// 试样编码输入框点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtCode_Click(object sender, EventArgs e)
        {
            txtCode.Text = "";
            txtWeight.Text = "";
        }

        /// <summary>
        /// 试样编码输入框改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtCode_TextChanged(object sender, EventArgs e)
        {
            txtWeight.Text = "";
            if (txtCode.TextLength == 9)
            {
                var strScode = txtCode.Text.Trim();
                try
                {
                    DateTime tmpDateTime; DateTime.TryParseExact(strScode.Substring(0, strScode.Length - 3), "yyMMdd", System.Globalization.CultureInfo.CurrentCulture, DateTimeStyles.None, out tmpDateTime);
                    DataTable tabSource = null;
                    var errMsg = DataOperate.DataQuery.DoQuerySampleInfo2(strScode, ref tabSource);
                    if (string.IsNullOrEmpty(errMsg))
                    {
                        if (tabSource != null && tabSource.Rows.Count > 0)
                        {
                            var strCollectCode = tabSource.Rows[0]["COLLECT_CODE"].ToString();//收样编码
                            var strType = tabSource.Rows[0]["SAMPLE_TYPE"].ToString();

                            if (_iAutoRecvAndPrintTestCode == 1)
                            {
                                var strErrInfo = string.Empty;
                                var strSampleType = "0";//样品类型
                                var strSampleNum = "0";//同类型样品数量
                                var samleNum = 0;
                                var bCheckResult = DataOperate.DataExecProc.DoCheckReceiveBatch(strScode, ref strSampleType, ref strSampleNum, ref strErrInfo);
                                if (!bCheckResult)
                                {
                                    FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("检查该批次是否已收样失败,原因:{0}", strErrInfo));
                                    return;
                                }
                                else
                                {
                                    try
                                    {
                                        samleNum = int.Parse(strSampleNum);
                                    }
                                    catch
                                    {
                                        FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("检查该批次已收样的同类型样品数量失败,数量为:{0}", strSampleNum));
                                        return;
                                    }
                                    if (samleNum > 0)
                                    {
                                        if (MessageBox.Show(string.Format("该试样编码【{0}】对应的批次已被化验室收取【{1}】样【{2}】份,请检查制样时编码是否录入错误！请确认是否继续添加？", strScode, strSampleType, samleNum), "！！！告警信息！！！", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                                        {
                                            return;
                                        }
                                    }

                                    var strTestCode = string.Empty;
                                    var bSaveResult = DataOperate.DataExecProc.DoAddReceiveSample(strScode, _strSendUserID, ref strTestCode, ref strErrInfo);
                                    if (!bSaveResult)
                                    {
                                        FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("添加化验编码失败,原因:{0}", strErrInfo));
                                        return;
                                    }
                                    else
                                    {
                                        FormHelper.ApplicationShow(ricMsg, DisplayType.Run, string.Format("添加化验编码《{0}》成功!", strTestCode));

                                        tabSource = null;
                                        errMsg = DataOperate.DataQuery.DoQueryReceiveSampleInfo("", strScode, "", _LabType, ref tabSource);
                                        if (string.IsNullOrEmpty(errMsg) && tabSource.Rows.Count == 1)
                                        {
                                            int i = 0;
                                            if (tabSource.Rows[i]["TYPE"].ToString() == "尚未配置化验模板")
                                            {
                                                FormHelper.ApplicationShow(ricMsg, DisplayType.Waring, string.Format("化验收样成功，但是未配置化验模板，请联系管理员配置后，重新查询出化验项目再手动打印二维码！"));
                                            }
                                            else
                                            {
                                                string needDisplayIndex = string.Empty;
                                                string displayIndex;
                                                var strCollectOrder = tabSource.Rows[i]["COLLECT_ORDER"].ToString();//分样号
                                                var strSampleCode = tabSource.Rows[i]["SAMPLE_CODE"].ToString();
                                                switch (System.Enum.Parse(typeof(SampleType), tabSource.Rows[i]["SAMPLE_TYPE"].ToString()))
                                                {
                                                    case SampleType.S13mm全水:
                                                        displayIndex = "Mt";
                                                        if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                                            needDisplayIndex = needDisplayIndex + "、" + displayIndex;
                                                        break;
                                                    case SampleType.S1_5mmY:
                                                        displayIndex = "Y";
                                                        if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                                            needDisplayIndex = needDisplayIndex + "、" + displayIndex;
                                                        break;
                                                    case SampleType.S1mm岩相:
                                                        displayIndex = "Rmax";
                                                        //if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                                        needDisplayIndex = needDisplayIndex + "、" + displayIndex;
                                                        break;
                                                    case SampleType.S0_425mm流动:
                                                        displayIndex = "MF";
                                                        //if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                                        needDisplayIndex = needDisplayIndex + "、" + displayIndex;
                                                        break;
                                                    case SampleType.S0_2mm工分:
                                                        displayIndex = "Ad";
                                                        if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                                            needDisplayIndex = needDisplayIndex + "、" + displayIndex;
                                                        displayIndex = "Vdaf";
                                                        if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                                            needDisplayIndex = needDisplayIndex + "、" + displayIndex;
                                                        displayIndex = "St,d";
                                                        if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                                            needDisplayIndex = needDisplayIndex + "、" + displayIndex;
                                                        break;
                                                    case SampleType.S0_2mmb:
                                                        displayIndex = "b";
                                                        if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                                            needDisplayIndex = needDisplayIndex + "、" + displayIndex;
                                                        break;
                                                    case SampleType.S0_2mmG:
                                                        displayIndex = "G";
                                                        if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                                            needDisplayIndex = needDisplayIndex + "、" + displayIndex;
                                                        break;
                                                    case SampleType.S0_2mm热值:
                                                        displayIndex = "Qnet,ar/Kcal";
                                                        if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                                            needDisplayIndex = needDisplayIndex + "、" + "Qnet,ar";
                                                        break;
                                                    case SampleType.S25mmCSR:
                                                        displayIndex = "CSR";
                                                        if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                                        {
                                                            if (tabSource.Rows[i]["MATERIEL_TYPE_NAME"].ToString() == "自检焦炭")
                                                            {
                                                                var strMakeTime = tabSource.Rows[0]["MAKE_TIME"].ToString();
                                                                needDisplayIndex = needDisplayIndex + "、" + "CSR-" + DateTime.Parse(strMakeTime).ToString("yyMMddHHmmss");
                                                            }
                                                            else
                                                                needDisplayIndex = needDisplayIndex + "、" + "CSR";
                                                        }
                                                        break;
                                                    case SampleType.S3mmTOD:
                                                        displayIndex = "TOD";
                                                        //if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                                        needDisplayIndex = needDisplayIndex + "、" + "TOD";
                                                        break;
                                                    default:
                                                        break;
                                                }
                                                if (!string.IsNullOrEmpty(needDisplayIndex))
                                                {
                                                    needDisplayIndex = needDisplayIndex.Substring(1);
                                                }
                                                DoPrint(strCollectOrder, strTestCode, strSampleCode, needDisplayIndex);
                                            }
                                        }
                                        else
                                        {
                                            FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("查询编码失败，请手动打印,原因:{0}", strErrInfo));
                                            return;
                                        }
                                        txtCode.Text = "";
                                        txtWeight.Text = "";
                                        dtDate.Value = DateTime.Now;
                                        dtDate.Checked = true;

                                        DoQuery("ADD");
                                        this.ActiveControl = txtCode;
                                        txtCode.Focus();
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("试样编码输入异常，原因：{0},请重新输入！", ex.Message));
                }
            }
        }
        /// <summary>
        /// 定时器清除送样人信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timerClera_Tick(object sender, EventArgs e)
        {
            txtSMan.Text = "";
            _strSendUserID = "";
        }

        /// <summary>
        /// 按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key.ToString())
            {
                case "ReadCardSF":
                    DoReadCardSF();
                    break;
                case "Query":
                    DoQuery("Normal");
                    break;
                case "Read":
                    DoRead();
                    break;
                case "Add":
                    DoAdd();
                    break;
                case "Print":
                    DoPrintView();
                    break;
                case "PrintAll":
                    DoPrintAll();
                    break;

            }
        }

        /// <summary>
        /// 身份验证
        /// </summary>
        private void DoReadCardSF()
        {
            timerClear.Enabled = false;
            txtSMan.Text = "";
            _strSendUserID = "";

            var strRfid = string.Empty;
            if (DoReadRfid(ref strRfid))
            {
                DataTable tabSource = null;
                var errMsg = DataOperate.DataQuery.DoQueryUserInfo(strRfid, ref tabSource);
                if (string.IsNullOrEmpty(errMsg))
                {
                    if (tabSource != null && tabSource.Rows.Count > 0)
                    {
                        _strSendUserID = tabSource.Rows[0]["ID"].ToString();
                        txtSMan.Text = tabSource.Rows[0]["TRUENAME"].ToString();
                        timerClear.Enabled = true;
                        timerClear.Start();
                        FormHelper.ApplicationShow(ricMsg, DisplayType.Run, string.Format("验证身份信息成功，卡号为：{0},姓名为：{1}", strRfid, tabSource.Rows[0]["TRUENAME"].ToString()));
                    }
                    else
                    {
                        FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("查询卡号：{0}的身份信息失败！原因：未找到对应的用户！", strRfid));
                    }
                }
                else
                {
                    FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("查询卡号：{0}的身份信息失败！原因：{1}", strRfid, errMsg));
                }
            }
        }

        /// <summary>
        /// 启动读卡
        /// </summary>
        private bool DoReadRfid(ref string strRfid)
        {
            var bSuccess = false;
            return bSuccess;
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        private void DoQuery(string QueryType)
        {
            timerClear.Interval = 60000;//未操作后60秒才清除
            _listSelectRow.Clear();

            DataTable tabSource = null;
            string strDate = string.Empty;
            if (dtDate.Checked)
                strDate = dtDate.Value.ToString("yyyy-MM-dd");
            var errMsg = DataOperate.DataQuery.DoQueryReceiveSampleInfo(strDate, txtCode.Text, _strSendUserID, _LabType, ref tabSource);
            if (string.IsNullOrEmpty(errMsg))
            {
                tabSource.Columns.Add("CHECK", typeof(bool));
                tabSource.Columns["CHECK"].ReadOnly = false;

                for (int i = 0; i < tabSource.Rows.Count; i++)
                {
                    string needDisplayIndex = string.Empty;
                    string displayIndex;
                    switch (System.Enum.Parse(typeof(SampleType), tabSource.Rows[i]["SAMPLE_TYPE"].ToString()))
                    {
                        case SampleType.S13mm全水:
                            displayIndex = "Mt";
                            if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                needDisplayIndex = needDisplayIndex + "、" + displayIndex;
                            break;
                        case SampleType.S1_5mmY:
                            displayIndex = "Y";
                            if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                needDisplayIndex = needDisplayIndex + "、" + displayIndex;
                            break;
                        case SampleType.S1mm岩相:
                            displayIndex = "Rmax";
                            //if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                            needDisplayIndex = needDisplayIndex + "、" + displayIndex;
                            break;
                        case SampleType.S0_425mm流动:
                            displayIndex = "MF";
                            //if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                            needDisplayIndex = needDisplayIndex + "、" + displayIndex;
                            break;
                        case SampleType.S0_2mm工分:
                            displayIndex = "Ad";
                            if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                needDisplayIndex = needDisplayIndex + "、" + displayIndex;
                            displayIndex = "Vdaf";
                            if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                needDisplayIndex = needDisplayIndex + "、" + displayIndex;
                            displayIndex = "St,d";
                            if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                needDisplayIndex = needDisplayIndex + "、" + displayIndex;
                            break;
                        case SampleType.S0_2mmb:
                            displayIndex = "b";
                            if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                needDisplayIndex = needDisplayIndex + "、" + displayIndex;
                            break;
                        case SampleType.S0_2mmG:
                            displayIndex = "G";
                            if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                needDisplayIndex = needDisplayIndex + "、" + displayIndex;
                            break;
                        case SampleType.S0_2mm热值:
                            displayIndex = "Qnet,ar/Kcal";
                            if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                                needDisplayIndex = needDisplayIndex + "、" + "Qnet,ar";
                            break;
                        case SampleType.S25mmCSR:
                            displayIndex = "CSR";
                            if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                            {
                                if (tabSource.Rows[i]["MATERIEL_TYPE_NAME"].ToString() == "自检焦炭")
                                {
                                    var strMakeTime = tabSource.Rows[0]["MAKE_TIME"].ToString();
                                    needDisplayIndex = needDisplayIndex + "、" + "CSR-" + DateTime.Parse(strMakeTime).ToString("yyMMddHHmmss");
                                }
                                else
                                    needDisplayIndex = needDisplayIndex + "、" + "CSR";
                            }
                            break;
                        case SampleType.S3mmTOD:
                            displayIndex = "TOD";
                            //if (tabSource.Rows[i]["TYPE"].ToString().Contains(displayIndex))
                            needDisplayIndex = needDisplayIndex + "、" + "TOD";
                            break;
                        default:
                            break;
                    }
                    if (!string.IsNullOrEmpty(needDisplayIndex))
                    {
                        needDisplayIndex = needDisplayIndex.Substring(1);
                        tabSource.Rows[i]["TYPE"] = needDisplayIndex;
                    }
                    else
                    {
                        if (tabSource.Rows[i]["TYPE"].ToString() != "尚未配置化验模板")
                            tabSource.Rows[i]["TYPE"] = "";
                    }
                }

                Titan.FIS.Data.MergeDataTable.MergeCaption(tabMain, ref tabSource);
                GridMain.DataSource = tabSource;
                if (QueryType == "Normal")
                    FormHelper.ApplicationShow(ricMsg, DisplayType.Run, "查询已完成！请在表格中查看相关数据");
            }
            else
            {
                GridMain.DataSource = tabMain;
                if (QueryType == "Normal")
                    FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("查询失败，原因:{0}", errMsg));
            }
        }

        /// <summary>
        /// RFID手动读卡
        /// </summary>
        private void DoRead()
        {
            timerClear.Interval = 60000;//未操作后60秒才清除
            txtCode.Text = "";
            txtWeight.Text = "";
            var strRfid = string.Empty;
            if (DoReadRfid(ref strRfid))
            {
                DataTable tabSource = null;
                var errMsg = DataOperate.DataQuery.DoQuerySampleInfo(strRfid, ref tabSource);
                if (string.IsNullOrEmpty(errMsg))
                {
                    if (tabSource != null && tabSource.Rows.Count > 0)
                    {
                        var strSCode = tabSource.Rows[0]["SAMPLE_CODE"].ToString();
                        var strType = tabSource.Rows[0]["SAMPLE_TYPE"].ToString();
                        float fWeight = 0; float.TryParse(tabSource.Rows[0]["WEIGHT"].ToString(), out fWeight);
                        txtCode.Text = strSCode;
                        if (Math.Abs(fWeight) > 0)
                        {
                            if (strType == "1")
                                txtWeight.Text = (fWeight + _iBottle6).ToString(CultureInfo.InvariantCulture);
                            else if (strType == "8")
                                txtWeight.Text = (fWeight + _iBottle3).ToString(CultureInfo.InvariantCulture);
                            else if (strType == "16")
                                txtWeight.Text = (fWeight + _iBottle2).ToString(CultureInfo.InvariantCulture);
                            else
                                txtWeight.Text = fWeight.ToString(CultureInfo.InvariantCulture);
                        }
                        FormHelper.ApplicationShow(ricMsg, DisplayType.Run, string.Format("读卡查询试样信息成功，卡号为：{0},试样编码为：{1}", strRfid, strSCode));
                    }
                    else
                    {
                        FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("查询卡号：{0}的试样信息失败！原因：未找到对应的试样信息！", strRfid));
                    }
                }
                else
                {
                    FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("查询卡号：{0}的试样信息失败！原因：{1}", strRfid, errMsg));
                }
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        private void DoAdd()
        {
            timerClear.Interval = 60000;//未操作后60秒才清除
            if (string.IsNullOrEmpty(txtCode.Text.Trim()))
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, "请先读取试样二维码值或者手动输入试样号!");
                return;
            }
            try
            {
                int code; int.TryParse(txtCode.Text.Trim(), out code);
                if (txtCode.Text.Trim().Length != 9)
                {
                    FormHelper.ApplicationShow(ricMsg, DisplayType.Error, "试样号输入错误，请重新输入");
                    return;
                }
            }
            catch
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, "试样号输入异常，请重新输入!");
                return;
            }

            var strQuerstion = string.Format("请确认是否生成试样【{0}】的化验编码？", txtCode.Text.Trim());
            if (MessageBox.Show(strQuerstion, "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                var strErrInfo = string.Empty;
                var strSampleType = "0";//样品类型
                var strSampleNum = "0";//同类型样品数量
                var samleNum = 0;
                var bCheckResult = DataOperate.DataExecProc.DoCheckReceiveBatch(txtCode.Text.Trim(), ref strSampleType, ref strSampleNum, ref strErrInfo);
                if (!bCheckResult)
                {
                    FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("检查该批次是否已收样失败,原因:{0}", strErrInfo));
                }
                else
                {
                    try
                    {
                        samleNum = int.Parse(strSampleNum);
                    }
                    catch
                    {
                        FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("检查该批次已收样的同类型样品数量失败,数量为:{0}", strSampleNum));
                        return;
                    }
                    if (samleNum > 0)
                    {
                        if (MessageBox.Show(string.Format("该试样编码【{0}】对应的批次已被化验室收取【{1}】样【{2}】份,请检查制样时编码是否录入错误！请确认是否继续添加？", txtCode.Text.Trim(), strSampleType, samleNum), "！！！告警信息！！！", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                        {
                            return;
                        }
                    }

                    var strTestCode = string.Empty;
                    var bSaveResult = DataOperate.DataExecProc.DoAddReceiveSample(txtCode.Text.Trim(), _strSendUserID, ref strTestCode, ref strErrInfo);
                    if (!bSaveResult)
                    {
                        FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("添加化验编码失败,原因:{0}", strErrInfo));
                    }
                    else
                    {
                        FormHelper.ApplicationShow(ricMsg, DisplayType.Run, string.Format("添加化验编码《{0}》成功!", strTestCode));
                        txtCode.Text = "";
                        txtWeight.Text = "";
                        dtDate.Value = DateTime.Now;
                        dtDate.Checked = true;

                        DoQuery("ADD");
                    }
                }
            }
        }

        /// <summary>
        /// 打印预览
        /// </summary>
        private void DoPrintView()
        {
            timerClear.Interval = 60000;//未操作后60秒才清除

            if (_listSelectRow.Count < 1)
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, "请在查询出来的数据表中，勾选需要打印的行在点击打印预览按钮，谢谢！");
                return;
            }
            if (_listSelectRow.Count > 1)
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, "只能选择一行数据进行预览，请重新选择,谢谢！");
                return;
            }

            var UltraGridRow = _listSelectRow[0];
            var strHCode = UltraGridRow.Cells["TEST_CODE"].Value.ToString();
            var strSCode = UltraGridRow.Cells["SAMPLE_CODE"].Value.ToString();
            var strCollectCode = UltraGridRow.Cells["COLLECT_CODE"].Value.ToString();
            var strCollectOrder = UltraGridRow.Cells["COLLECT_ORDER"].Value.ToString();
            var strType = UltraGridRow.Cells["TYPE"].Value.ToString();//打印显示用

            var obj = new frmView(strHCode, strSCode, strType, "3", _iPrintTestCodeNum, strCollectOrder, _iPrintBarcodeType);
            obj.ShowDialog();
        }


        /// <summary>
        /// 批量打印
        /// </summary>
        private void DoPrintAll()
        {
            timerClear.Interval = 60000;//未操作后60秒才清除

            if (_listSelectRow.Count < 1)
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, "请在查询出来的数据表中，勾选需要打印的行在点击打印预览按钮，谢谢！");
                return;
            }

            foreach (var UltraGridRow in _listSelectRow)
            {
                var strHCode = UltraGridRow.Cells["TEST_CODE"].Value.ToString();
                var strCollectCode = UltraGridRow.Cells["COLLECT_CODE"].Value.ToString();
                var strCollectOrder = UltraGridRow.Cells["COLLECT_ORDER"].Value.ToString();
                var strSampleCode = UltraGridRow.Cells["SAMPLE_CODE"].Value.ToString();
                var strType = UltraGridRow.Cells["TYPE"].Value.ToString();//打印显示用
                DoPrint(strCollectOrder, strHCode, strSampleCode, strType);
            }
            FormHelper.ApplicationShow(ricMsg, DisplayType.Run, "批量打印完毕，谢谢！");
        }

        /// <summary>
        /// 打印标签
        /// </summary>
        /// <param name="strCollectCode">收样编码</param>
        /// <param name="strCode">打印编码</param>
        /// <param name="strSCode">匹配编码</param>
        /// <param name="strType">样品类型 H3-H6-H2-H0</param>
        private void DoPrint(string strCollectCode, string strCode, string strSCode, string strType)
        {
            Image img;
            if (_iPrintBarcodeType == 1)
            {
                var gen = new TagGen();
                img = gen.GetImgASample(strType, strCode, _iPrintTestCodeNum);
                strType = strCollectCode + "_" + strType;
            }
            else
            {
                var strDesc = strCollectCode.Length <= 2 && !string.IsNullOrEmpty(strCollectCode) ? strCollectCode + "号" : strCollectCode;
                var strDescSize = strDesc.Length <= 2 ? "60" : (strDesc.Length * 2 * 10).ToString();
                img = Barcode.TwoDimensionBarcode.Encode(QRCodeEncoder.ENCODE_MODE.BYTE, QRCodeEncoder.ERROR_CORRECTION.M, "4", "7", strCode, "", strDesc, strDescSize);
            }
            img = Barcode.TwoDimensionBarcode.CombinImageFloor(img, strType, 10, 80);
            var obj = new TagPrint(img, _iPrintBarcodeType);
            if (!obj.Print())
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("打印标签号{0}失败!", strCode));
            }
            else
            {
                var strErrInfo = string.Empty;
                var bSaveResult = DataOperate.DataExecProc.DoUpdatePrintCount(strSCode, "3", ref strErrInfo);
                if (!bSaveResult)
                {
                    FormHelper.ApplicationShow(ricMsg, DisplayType.Error, string.Format("更新编码{0}打印次数失败！原因：{1}", strCode, strErrInfo));
                }
                else
                    FormHelper.ApplicationShow(ricMsg, DisplayType.Run, string.Format("打印编码{0}完毕，谢谢！", strCode));
            }
        }




    }
}
